package cn.school.component.aspect;

import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.hutool.core.date.DateUtil;
import cn.school.async.AsyncConfig;
import cn.school.classManage.dao.SchoolClassDao;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.rabbitMQ.RabbitSenderService;
import cn.school.common.utils.SysUserUtils;
import cn.school.notice.dao.FamilyNoticeDao;
import cn.school.notice.entity.FamilyNotice;
import cn.school.notice.entity.WorkflowNotice;
import cn.school.notice.service.WorkflowNoticeService;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.entity.StudentInfo;
import cn.school.sys.dao.SysUserDao;
import cn.school.sys.entity.SysUser;
import cn.school.workflow.dao.*;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 流程处理时找到审批人
 */
@Aspect
@Component
@Slf4j
public class ActivityAssigneeAspect {
    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    @Autowired
    private LeaveApplyDao leaveApplyDao;

    @Autowired
    private QuitApplyDao quitApplyDao;
    @Autowired
    private UsePersonApplyDao usePersonApplyDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private DocumentApplyDao documentApplyDao;
    @Autowired
    private ViolativeApplyDao violativeApplyDao;
    @Autowired
    private RepairApplyDao repairApplyDao;
    @Resource
    private VisitApplyDao visitApplyDao;
    @Resource
    private MeetingReservedApplyDao meetingReservedApplyDao;
    @Resource
    private ProcessEngine processEngine;
    @Resource
    private WorkflowNoticeService workflowNoticeService;
    @Resource
    private StudentLeaveApplyDao studentLeaveApplyDao;
    @Resource
    private GoodsBuyApplyDao goodsBuyApplyDao;
    @Resource
    private StudentLeaveSelfApplyDao studentLeaveSelfApplyDao;
    @Resource
    private AsyncConfig asyncConfig;
    @Resource
    private SchoolClassDao schoolClassDao;
    @Resource
    private FamilyNoticeDao familyNoticeDao;
    @Resource
    private RabbitSenderService rabbitSenderService;

    @Before("execution(* cn.school.workflow.service.impl.WorkFlowCommonServiceImpl.processWorkFlow(..))")
    public void assigneeBefore(JoinPoint point) {
        WorkFlowVO vo = (WorkFlowVO)point.getArgs()[0];
        if(vo.getApplyID().startsWith("leaveApply")) {
            //设置请假申请的审批人
            setLeaveCurrApprovedBy(vo);
        }
        if("violativeApply".equals(vo.getApplyID())){
            if(vo.getCurrNode().equals("distHeadle")){
                Map<String,Object> variables = new HashMap<>();
                variables.put("assignee","test2");
                vo.setVariables(variables);
            }
        }
        //维修
        if(vo.getApplyID().startsWith("repairApply")){
            setRepairApprovedBy(vo);
        }
        //离职
        if(vo.getApplyID().startsWith("quitApply")){
            setQuitCurrApprovedBy(vo);
        }
        //部门用人申请
        if(vo.getApplyID().startsWith("usePersonApply")){
            setUsePersonApprovedBy(vo);
        }
        //学生请假（代提）
        if(vo.getApplyID().startsWith("studentLeaveWithdrawal")){
            setStudentLeaveWithdrawalApprovedBy(vo);
        }
        //学生请假（自提）
        if(vo.getApplyID().startsWith("studentLeaveSelfApply")){
            setstudentLeaveSelfApplyApprovedBy(vo);
        }
        //物品申购
        if(vo.getApplyID().startsWith("goodsBuyApply")){
            setGoodsBuyApplyApprovedBy(vo);
        }
    }
    private void setGoodsBuyApplyApprovedBy(WorkFlowVO vo){
        LambdaQueryWrapper<GoodsBuyApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsBuyApplyVO::getProcessId, vo.getProcessInstanceId());
        GoodsBuyApplyVO applyVO = new GoodsBuyApplyVO();

        if(vo.getCurrNode().equals("deptLeader")) {//部门领导
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("执行校长");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", list.get(0));
            vo.setVariables(variables);
            applyVO.setCurrAppprovedBy(list.get(0));
            goodsBuyApplyDao.update(applyVO,wrapper);
        }
        if(vo.getCurrNode().equals("executePrincipal")) {//执行校长
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("后勤部");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", list.get(0));
            vo.setVariables(variables);
            applyVO.setCurrAppprovedBy(list.get(0));
            goodsBuyApplyDao.update(applyVO,wrapper);
        }
    }
    /**
     * 设置维修确认审批人
     * @param vo
     */
    private void setRepairApprovedBy(WorkFlowVO vo) {
        if(vo.getCurrNode().equals("repairConfirm") && vo.getFlag()==1){
            LambdaQueryWrapper<RepairApplyVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RepairApplyVO::getProcessId, vo.getProcessInstanceId());
            RepairApplyVO repairVO = repairApplyDao.selectOne(wrapper);
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee",repairVO.getCreateBy());
            vo.setVariables(variables);
            RepairApplyVO updateVO = new RepairApplyVO();
            updateVO.setCurrApprovedBy(repairVO.getCreateBy());
            repairApplyDao.update(updateVO,wrapper);
        }
    }


    @AfterReturning(returning="desReturnValue",pointcut="execution(* cn.school.workflow.service.impl.WorkFlowCommonServiceImpl.processWorkFlow(..))")
    public void processAfterReturning(Object desReturnValue) {
        //打印返回值对象
        System.out.println(String.valueOf(desReturnValue));
        WorkFlowVO vo = (WorkFlowVO)desReturnValue;
        //请假流程
        if(vo.getApplyID().startsWith("leaveApply")){
            processLeaveApply(vo);
        }
        //离职流程
        if(vo.getApplyID().startsWith("quitApply")){
            processQuitApply(vo);

        }
        //部门用人流程
        if(vo.getApplyID().startsWith("usePersonApply")){
            usePersonApply(vo);

        }
        //维修流程
        if(vo.getApplyID().startsWith("repairApply")){
            repairApply(vo);

        }
        //学生请假（代提）
        if(vo.getApplyID().startsWith("studentLeaveWithdrawal")){
            studentLeaveWithdrawalApply(vo);
        }
        //学生请假（自提）
        if(vo.getApplyID().startsWith("studentLeaveSelfApply")){
            studentLeaveSelfApply(vo);
        }
        //申购流程
        if(vo.getApplyID().startsWith("goodsBuyApply")){
            goodsBuyApply(vo);
        }

        //公文流程
        if("documentApply".equals(vo.getApplyID())){
            processDocumentApply(vo);
        }
        //违纪流程
        if("violativeApply".equals(vo.getApplyID())){
            processViolativeApply(vo);
        }
        //访客流程
        if("visitApply".equals(vo.getApplyID())){
            processVisitApply(vo);
        }
        //会议预定流程
        if("meetingApply".equals(vo.getApplyID())){
            processMeetingApply(vo);
        }


    }
    private void repairApply(WorkFlowVO vo){
        LambdaQueryWrapper<RepairApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairApplyVO::getProcessId, vo.getProcessInstanceId());
        RepairApplyVO repairVO = repairApplyDao.selectOne(wrapper);
        if(vo.getCurrNode().equals("repairConfirm")) {//维修确认
            RepairApplyVO updateVO = new RepairApplyVO();
            if(0==vo.getFlag()){
                updateVO.setApplyStatus("2");//驳回流程,
                updateVO.setCurrApprovedBy("");
                repairApplyDao.update(updateVO,wrapper);
                addRepairFlowNotice(repairVO,false);
            }
        }
        if(vo.getCurrNode().equals("repairerConfirm")) {//报修人确认
            RepairApplyVO updateVO = new RepairApplyVO();
            updateVO.setCurrApprovedBy("");
            if(0==vo.getFlag()){
                updateVO.setApplyStatus("2");//驳回流程,
                addRepairFlowNotice(repairVO,false);
            }else{
                updateVO.setApplyStatus("3");//流程介绍,
                addRepairFlowNotice(repairVO,true);
            }
            repairApplyDao.update(updateVO,wrapper);

        }
    }
    /**
     * 学生请假代提审批人处理
     * @param vo
     */
    private void setStudentLeaveWithdrawalApprovedBy(WorkFlowVO vo){
        // 根据部门id查询出部门下人员列表
/*        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getDeptId, vo.getDeptId());
        List<SysUser> users = sysUserDao.selectList(lambdaQueryWrapper);
        List<String> userNames = users.stream().map(SysUser::getUsername).collect(Collectors.toList());

        Map<String,Object> variables = new HashMap<>();
        variables.put("candidateUsers", userNames);
        vo.setVariables(variables);
        studentLeaveApplyDao.update(applyVO,wrapper);
        //小于等于1天的请假只要班主任审批，没有下级审批人
        applyVO.setCurrApprovedBy(StringUtils.join(userNames,","));

        LambdaQueryWrapper<StudentLeaveApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentLeaveApplyVO::getProcessId, vo.getProcessInstanceId());
        StudentLeaveApplyVO applyVO = new StudentLeaveApplyVO();
        if(vo.getCurrNode().equals("gradeApproval")) {//学工负责人审批
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("学工");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            vo.setVariables(variables);
            applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            studentLeaveApplyDao.update(applyVO,wrapper);
        }*/

    }

    /**
     * 学生请假（自提）
     * @param vo
     */
    private void setstudentLeaveSelfApplyApprovedBy(WorkFlowVO vo){
        // 根据部门id查询出部门下人员列表
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getDeptId, vo.getDeptId());
        List<SysUser> users = sysUserDao.selectList(lambdaQueryWrapper);
        List<String> userNames = users.stream().map(SysUser::getUsername).collect(Collectors.toList());

        LambdaQueryWrapper<StudentLeaveSelfApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentLeaveSelfApplyVO::getProcessId, vo.getProcessInstanceId());
        StudentLeaveSelfApplyVO applyVO = new StudentLeaveSelfApplyVO();
        StudentLeaveSelfApplyVO leaveVO = studentLeaveSelfApplyDao.selectOne(wrapper);
        Double days = Double.valueOf(leaveVO.getLeaveTotalTime());
        if(vo.getCurrNode().equals("classTeacher")) {//班主任审批前更新下级审批人
            if("1".equals(vo.getTenantId())){
                // 江西长江理工
                Map<String,Object> variables = new HashMap<>();
                variables.put("candidateUsers", userNames);
                variables.put("days", days);
                vo.setVariables(variables);

                //小于等于1天的请假只要班主任审批，没有下级审批人
                applyVO.setCurrApprovedBy(StringUtils.join(userNames,","));
                studentLeaveSelfApplyDao.update(applyVO,wrapper);
                for (String userName : userNames) {
                    sendWechatSmallMessage(leaveVO.getStudentName(),userName);
                }
            }else if("9".equals(vo.getTenantId())){
                // 长沙经贸中专
                userNames = sysUserDao.getSysUserNamesByRoleName(vo.getTenantId(),"学工");

                Map<String,Object> variables = new HashMap<>();
                variables.put("candidateUsers", userNames);
                variables.put("days", days);
                vo.setVariables(variables);

                applyVO.setCurrApprovedBy(StringUtils.join(userNames,","));
                studentLeaveSelfApplyDao.update(applyVO,wrapper);


            }

        }
  /*      if(vo.getCurrNode().equals("gradeApproval")) {//年级主任审批前更新下级审批人
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("学工");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            variables.put("days", days);
            vo.setVariables(variables);
            if(days>2){
                applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            }else{
                //小于等于2天的请假只要班主任审批，没有下级审批人
                applyVO.setCurrApprovedBy("");
            }

            studentLeaveSelfApplyDao.update(applyVO,wrapper);
        }*/

    }
    private void sendWechatSmallMessage(String name, String approvalUser) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, approvalUser);
        SysUser user = sysUserDao.selectOne(wrapper);
        List<WxMaTemplateData> dataList = new ArrayList<>();
        WxMaTemplateData data1 = new WxMaTemplateData("thing1", "有新的申请单需要你审批");
        WxMaTemplateData data2 = new WxMaTemplateData("thing14", name);
        WxMaTemplateData data3 = new WxMaTemplateData("time15", DateUtil.formatDateTime(new Date()));
        dataList.add(data1);
        dataList.add(data2);
        dataList.add(data3);
        asyncConfig.sendWxMessage(user.getOpenId(),"EeC_75iP_ZDroTpzdxYdrAuZyH4dhV_JfCGR2MSjPQA",dataList,"pages/office/office");
    }

    /**
     * 设置用人申请审批人
     * @param vo
     */
    private void setUsePersonApprovedBy(WorkFlowVO vo){
        LambdaQueryWrapper<UsePersonApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsePersonApplyVO::getProcessId, vo.getProcessInstanceId());
        UsePersonApplyVO applyVO = new UsePersonApplyVO();

        if(vo.getCurrNode().equals("inChargePrincipal")) {//分管校长
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("人事部");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            vo.setVariables(variables);
            applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            usePersonApplyDao.update(applyVO,wrapper);
        }
        if(vo.getCurrNode().equals("hr")) {//人事
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("执行校长");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            vo.setVariables(variables);
            applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            usePersonApplyDao.update(applyVO,wrapper);
        }
    }
    private void usePersonApply(WorkFlowVO vo){
        LambdaQueryWrapper<UsePersonApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsePersonApplyVO::getProcessId, vo.getProcessInstanceId());
        UsePersonApplyVO applyVO = new UsePersonApplyVO();
        if(vo.getCurrNode().equals("inChargePrincipal")) {//分管校长
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程,
                applyVO.setCurrApprovedBy("");
                usePersonApplyDao.update(applyVO,wrapper);
            }

        }
        if(vo.getCurrNode().equals("hr")) {//人事
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程,
                applyVO.setCurrApprovedBy("");
                usePersonApplyDao.update(applyVO,wrapper);
            }

        }
        if(vo.getCurrNode().equals("executePrincipal")) {//执行校长
            UsePersonApplyVO usePersonVO = usePersonApplyDao.selectOne(wrapper);
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程,
                addUsePersonFlowNotice(usePersonVO,false);
            }
            if(1==vo.getFlag()){
                applyVO.setStatus("3");//审批通过，流程结束,
                addUsePersonFlowNotice(usePersonVO,true);
            }
            applyVO.setCurrApprovedBy("");
            usePersonApplyDao.update(applyVO,wrapper);
        }

    }
    /**
     * 设置请假申请审批人
     * @param vo
     */
    private void setLeaveCurrApprovedBy(WorkFlowVO vo) {
        LambdaQueryWrapper<LeaveApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaveApplyVO::getProcessId, vo.getProcessInstanceId());
        LeaveApplyVO applyVO = new LeaveApplyVO();
        LeaveApplyVO leaveVO = leaveApplyDao.selectOne(wrapper);
        Integer days = Integer.valueOf(leaveVO.getLeaveTotalTime());
        if(vo.getCurrNode().equals("leaderApprovalCountersign")){//上级会签
            Map<String, Object> map = processCountersign(vo.getProcessInstanceId(),vo.getTaskId(),vo.getVariables(),vo.getFlag());
            map.put("days", days);
            int passCount = Integer.valueOf(map.get("passCount")+"");
            int totalCount = Integer.valueOf(map.get("totalCount")+"");
            if(passCount==totalCount){
                List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("分管校长");
                map.put("assignee0", StringUtils.join(list,","));
                vo.setVariables(map);
                if(days>1){
                    //回写流程下级审批人
                    applyVO.setCurrApprovedBy(StringUtils.join(list,","));
                }else{
                    applyVO.setCurrApprovedBy("");
                }
                leaveApplyDao.update(applyVO,wrapper);
            }else{
                map.put("assignee", SysUserUtils.getSysUser().getUsername());
            }
        }
        if(vo.getCurrNode().equals("inChargePrincipal")){//分管校长
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("执行校长");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            variables.put("days", days);
            vo.setVariables(variables);
            if(days>2){
                //回写流程下级审批人
                applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            }else{
                applyVO.setCurrApprovedBy("");
            }

            leaveApplyDao.update(applyVO,wrapper);
        }

    }
    /**
     * 设置离职申请审批人
     * @param vo
     */
    private void setQuitCurrApprovedBy(WorkFlowVO vo) {
        LambdaQueryWrapper<QuitApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuitApplyVO::getProcessId, vo.getProcessInstanceId());
        QuitApplyVO applyVO = new QuitApplyVO();
        if(vo.getCurrNode().equals("leaderApprovalCountersign")){//上级会签
            Map<String, Object> map = processCountersign(vo.getProcessInstanceId(),vo.getTaskId(),vo.getVariables(),vo.getFlag());

            int passCount = Integer.valueOf(map.get("passCount")+"");
            int totalCount = Integer.valueOf(map.get("totalCount")+"");
            if(passCount==totalCount){
                List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("分管校长");
                map.put("assignee0", StringUtils.join(list,","));
                vo.setVariables(map);
                //回写流程当前审批人
                applyVO.setCurrApprovedBy(StringUtils.join(list,","));
                quitApplyDao.update(applyVO,wrapper);
            }else{
                map.put("assignee", SysUserUtils.getSysUser().getUsername());
            }
        }
        if(vo.getCurrNode().equals("inChargePrincipal")){//分管校长
            List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("执行校长");
            Map<String,Object> variables = new HashMap<>();
            variables.put("assignee", StringUtils.join(list,","));
            vo.setVariables(variables);
            //回写流程当前审批人
            applyVO.setCurrApprovedBy(StringUtils.join(list,","));
            quitApplyDao.update(applyVO,wrapper);
        }

    }
    /**
     * 处理会签
     */
    private Map<String, Object> processCountersign(String procInstId,String taskId, Map<String, Object> variables,int passflag){
        Task taskQuery = processEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();

        List<Task> tasks = processEngine.getTaskService().createTaskQuery().taskName(taskQuery.getName()).processInstanceId(procInstId).list();
        int passCount = 0;//审批同意人数
        int noPassCount = 0;//审批不同意人数
        int totalCount = 0;//任务总人数
        //当前的执行情况
        String tmpPassCount = processEngine.getRuntimeService().getVariable(procInstId, taskQuery.getTaskDefinitionKey()+"#passCount")+"";
        String tmpNoPassCount = processEngine.getRuntimeService().getVariable(procInstId, taskQuery.getTaskDefinitionKey()+"#noPassCount")+"";
        String tmpTotal = processEngine.getRuntimeService().getVariable(procInstId, taskQuery.getTaskDefinitionKey()+"#totalCount")+"";
        if(!tmpPassCount.equals("null") && !tmpPassCount.trim().equals("")){
            passCount = Integer.parseInt(tmpPassCount);
        }
        if(!tmpNoPassCount.equals("null") && !tmpNoPassCount.trim().equals("")){
            noPassCount = Integer.parseInt(tmpNoPassCount);
        }
        if(tmpTotal.equals("null") || tmpTotal.trim().equals("")){
            totalCount = tasks.size();
        } else if(!tmpTotal.equals("null") && !tmpTotal.trim().equals("")){
            totalCount = Integer.parseInt(tmpTotal);
        }
        for (Task tmp:tasks) {
            if(passflag==1 && tmp.getId().equals(taskId)){//选择通过则通过人数+1
                passCount++;
            }
            if(passflag==0 && tmp.getId().equals(taskId)){//选择不通过则不通过人数+1
                noPassCount++;
            }
        }
        //变量回写记录
        variables.put("passCount", passCount);
        variables.put("noPassCount", noPassCount);
        variables.put("totalCount", totalCount);
        variables.put(taskQuery.getTaskDefinitionKey()+"#passCount", passCount);
        variables.put(taskQuery.getTaskDefinitionKey()+"#noPassCount", noPassCount);
        variables.put(taskQuery.getTaskDefinitionKey()+"#totalCount", totalCount);
        return variables;
    }
    /**
     * 会议预定
     * @param vo
     */
    private void processMeetingApply(WorkFlowVO vo){
        LambdaQueryWrapper<MeetingReservedApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingReservedApplyVO::getProcessId,vo.getProcessInstanceId());
        MeetingReservedApplyVO meetingReservedApplyVO = new MeetingReservedApplyVO();
        if("logisticsApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                meetingReservedApplyVO.setApplyStatus("3");//驳回流程,
            }else{
                meetingReservedApplyVO.setApplyStatus("4");//处理完成,
            }
            meetingReservedApplyDao.update(meetingReservedApplyVO,wrapper);
        }
        if("managerApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                meetingReservedApplyVO.setApplyStatus("3");//驳回流程,
            }else{
                meetingReservedApplyVO.setApplyStatus("4");//处理完成,
            }
            meetingReservedApplyDao.update(meetingReservedApplyVO,wrapper);
        }
    }


    /**
     * 处理访客
     * @param vo
     */
    private void processVisitApply(WorkFlowVO vo){
        LambdaQueryWrapper<VisitApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VisitApplyVO::getProcessId,vo.getProcessInstanceId());
        VisitApplyVO visitApplyVO = new VisitApplyVO();
        if("deptApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                visitApplyVO.setApplyStatus("3");//驳回流程,
            }else{
                visitApplyVO.setApplyStatus("4");//处理完成,
            }
            visitApplyDao.update(visitApplyVO,wrapper);
        }
        if("securityApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                visitApplyVO.setApplyStatus("3");//驳回流程,
            }else{
                visitApplyVO.setApplyStatus("4");//处理完成,
            }
            visitApplyDao.update(visitApplyVO,wrapper);
        }

    }

    /**
     * 处理违纪
     * @param vo
     */
    private void processViolativeApply(WorkFlowVO vo){
        LambdaQueryWrapper<ViolativeApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ViolativeApplyVO::getProcessId,vo.getProcessInstanceId());
        ViolativeApplyVO violativeApplyVO = new ViolativeApplyVO();

        if("violativeHeadle".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                violativeApplyVO.setApplyStatus("3");//驳回流程,
            }else{
                violativeApplyVO.setApplyStatus("4");//处理完成,
                //违纪处理完成后通知申请单提交人 TODO
            }
            violativeApplyDao.update(violativeApplyVO,wrapper);
        }
    }
    /**
     * 处理公文流程
     * @param vo
     */
    private void processDocumentApply(WorkFlowVO vo){
        LambdaQueryWrapper<DocumentApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocumentApplyVO::getProcessId,vo.getProcessInstanceId());
        DocumentApplyVO documentApplyVO = new DocumentApplyVO();
        if("oneLevelApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                documentApplyVO.setApplyStatus("3");//驳回流程,
                documentApplyDao.update(documentApplyVO,wrapper);
            }

        }
        if("twoLevelApproval".equals(vo.getCurrNode())){
            documentApplyVO.setApplyStatus("4");//处理完成,
            documentApplyDao.update(documentApplyVO,wrapper);
        }
    }

    /**
     * 查询人事审批人
     * @return
     */
    private void assigneePersonnelApproval(WorkFlowVO vo){

        List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("人事部");
        if(list!=null){
            Map<String,Object> variables = vo.getVariables();
            String assignee = Joiner.on(",").join(list);
            if(variables==null){
                variables = new HashMap<>();
            }
            variables.put("assignee",assignee);
            vo.setVariables(variables);
        }

    }
    /**
     * 申购审批处理
     * @param vo
     */
    private void goodsBuyApply(WorkFlowVO vo){
        LambdaQueryWrapper<GoodsBuyApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsBuyApplyVO::getProcessId,vo.getProcessInstanceId());
        GoodsBuyApplyVO leaveVO = goodsBuyApplyDao.selectOne(wrapper);
        if("deptLeader".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setApplyStatus("2");//驳回流程结束
                leaveVO.setCurrAppprovedBy("");
                addGoodsBuyNotice(leaveVO,false);
                goodsBuyApplyDao.update(leaveVO,wrapper);
            }
        }
        if("executePrincipal".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setApplyStatus("2");//驳回流程结束
                leaveVO.setCurrAppprovedBy("");
                addGoodsBuyNotice(leaveVO,false);
                goodsBuyApplyDao.update(leaveVO,wrapper);
            }
        }
        if("logistics".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setApplyStatus("2");//驳回流程结束
                leaveVO.setCurrAppprovedBy("");
                addGoodsBuyNotice(leaveVO,false);
                goodsBuyApplyDao.update(leaveVO,wrapper);
            }
            if(1==vo.getFlag()){
                leaveVO.setApplyStatus("3");//流程正常结束
                leaveVO.setCurrAppprovedBy("");
                addGoodsBuyNotice(leaveVO,true);
                goodsBuyApplyDao.update(leaveVO,wrapper);
            }
        }
    }
    private void studentLeaveSelfApply(WorkFlowVO vo){
        LambdaQueryWrapper<StudentLeaveSelfApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentLeaveSelfApplyVO::getProcessId,vo.getProcessInstanceId());
        StudentLeaveSelfApplyVO leaveVO = studentLeaveSelfApplyDao.selectOne(wrapper);
        Double days = Double.valueOf(leaveVO.getLeaveTotalTime());

        StudentLeaveSelfApplyVO applyVO = new StudentLeaveSelfApplyVO();
        if("classTeacher".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程结束
                applyVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,false);
                studentLeaveSelfApplyDao.update(applyVO,wrapper);
            }
//            if(1==vo.getFlag() && days<=1){
            if(1==vo.getFlag()){
                //不同租户走不同流程
                if("1".equals(vo.getTenantId()) && days<=1){
                    // 长江理工 请假天数小于1填
                    applyVO.setStatus("3");//流程正常结束
                    applyVO.setCurrApprovedBy("");
                    addStudentLeaveSelfNotice(leaveVO,true);
                    studentLeaveSelfApplyDao.update(applyVO,wrapper);
                    //推送请假到IOT
//                    sendMessageToIot(leaveVO);
                }else if(!("1".equals(vo.getTenantId()) || "9".equals(vo.getTenantId()))){
                    // 不是长江理工和长沙经贸中专学校
                    applyVO.setStatus("3");//流程正常结束
                    applyVO.setCurrApprovedBy("");
                    addStudentLeaveSelfNotice(leaveVO,true);
                    studentLeaveSelfApplyDao.update(applyVO,wrapper);
                    //推送请假到IOT
                    sendMessageToIot(leaveVO);
                }
            }
        }
        if("gradeApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setStatus("2");//驳回流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,false);
                studentLeaveSelfApplyDao.update(leaveVO,wrapper);
            }
            if(1==vo.getFlag() && days<=2){
                applyVO.setStatus("3");//流程正常结束
                applyVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,true);
                studentLeaveSelfApplyDao.update(applyVO,wrapper);
                //推送请假到IOT
//                sendMessageToIot(leaveVO);
            }
        }
        if("ssApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setStatus("2");//驳回流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,false);
                studentLeaveSelfApplyDao.update(leaveVO,wrapper);
            }
//            if(1==vo.getFlag() && days>=3){
            if(1==vo.getFlag()){
                leaveVO.setStatus("3");//流程正常结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,true);
                studentLeaveSelfApplyDao.update(leaveVO,wrapper);
                //推送请假到IOT
//                sendMessageToIot(leaveVO);
            }
        } else if("sgApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setStatus("2");//驳回流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,false);
                studentLeaveSelfApplyDao.update(leaveVO,wrapper);
            }
            if(1==vo.getFlag()){
                leaveVO.setStatus("3");//流程正常结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveSelfNotice(leaveVO,true);
                studentLeaveSelfApplyDao.update(leaveVO,wrapper);
                //推送请假到IOT
                sendMessageToIot(leaveVO);
            }
        }
    }

    private void sendMessageToIot(StudentLeaveSelfApplyVO leaveVO) {
        LambdaQueryWrapper<StudentInfo> studentInfoWrapper = new LambdaQueryWrapper<>();
        studentInfoWrapper.eq(StudentInfo::getStudentId, leaveVO.getStudentId());
        StudentInfo studentInfo = studentInfoDao.selectOne(studentInfoWrapper);
        leaveVO.setStudentInfo(studentInfo);
        String tenantId = studentInfo.getTenantId();
        if(!StringUtils.isEmpty(tenantId)){
            //发送消息给MQ TODO
            rabbitSenderService.sendMqMessage(JSON.toJSONString(leaveVO), null,
                    "zhxypt-" + tenantId + "-exchanges",
                    "zhxypt-" + tenantId + "-routing");
        }else {
            log.info(" ActivityAssigneeAspect ===> 找不到租户ID,未推送消息至IOT");
        }
       //发送消息给MQ TODO
//        rabbitSenderService.sendMqMessage(JSON.toJSONString(leaveVO), null);
    }
    private void sendLeaveMessageToIot(StudentLeaveApplyVO leaveVO) {
        LambdaQueryWrapper<StudentInfo> studentInfoWrapper = new LambdaQueryWrapper<>();
        studentInfoWrapper.eq(StudentInfo::getStudentId, leaveVO.getStudentId());
        StudentInfo studentInfo = studentInfoDao.selectOne(studentInfoWrapper);
        leaveVO.setStudentInfo(studentInfo);
        //发送消息给MQ TODO
        rabbitSenderService.sendMqMessage(JSON.toJSONString(leaveVO), null);
    }

    /**
     * 学生请假（代提）审批处理
     * @param vo
     */
    private void studentLeaveWithdrawalApply(WorkFlowVO vo){
        LambdaQueryWrapper<StudentLeaveApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentLeaveApplyVO::getProcessId,vo.getProcessInstanceId());
        StudentLeaveApplyVO leaveVO = studentLeaveApplyDao.selectOne(wrapper);
        if("gradeApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setStatus("2");//驳回流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveNotice(leaveVO,false);
                studentLeaveApplyDao.update(leaveVO,wrapper);
            }
        }
        if("ssApproval".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                leaveVO.setStatus("2");//驳回流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveNotice(leaveVO,false);
                studentLeaveApplyDao.update(leaveVO,wrapper);
            }
            if(1==vo.getFlag()){
                leaveVO.setStatus("3");//通过流程结束
                leaveVO.setCurrApprovedBy("");
                addStudentLeaveNotice(leaveVO,true);
                studentLeaveApplyDao.update(leaveVO,wrapper);
//                sendLeaveMessageToIot(leaveVO);
            }

        }
    }

    @Resource
    private StudentInfoDao studentInfoDao;
    private void addStudentLeaveSelfNotice(StudentLeaveSelfApplyVO vo,boolean flag){
        List<String> sendUserList = new ArrayList<>();

        LambdaQueryWrapper<StudentInfo> studentWrapper = new LambdaQueryWrapper<>();
        studentWrapper.eq(StudentInfo::getStudentId,vo.getStudentId());
        StudentInfo sInfo =  studentInfoDao.selectOne(studentWrapper);
        sendUserList.add(sInfo.getStudentId());

        String title = "["+sInfo.getName()+"]的学生请假申请审批不通过";
        String content = "["+sInfo.getName()+"]提交的学生请假申请单审批不通过，具体内容请查看请假申请单"+vo.getApplyNo()+"的审批记录";
        if(flag){
            if(!org.springframework.util.StringUtils.isEmpty(vo.getCcUsers())){
                List<String> ccUserList = Arrays.asList(vo.getCcUsers().split(","));
                sendUserList.addAll(ccUserList);
            }
            title = vo.getClassName()+"班的["+sInfo.getName()+"]的学生请假申请审批通过";
            content = "["+sInfo.getName()+"]提交学生请假申请单已经审批通过，请假时间为:"+vo.getLeaveStartTime()+"~"+vo.getLeaveEndTime();

        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //学生请假通过通知老师，家长，学生
        jgMessageStudent(title,content,sInfo);
    }

    private void addStudentLeaveNotice(StudentLeaveApplyVO vo,boolean flag){
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(vo.getCreateBy());
        String title = "["+vo.getStudentName()+"]的学生请假申请审批不通过";
        String content = "["+vo.getCreateBy()+"]提交的学生["+vo.getStudentName()+"]请假申请单审批不通过，具体内容请查看请假申请单"+vo.getApplyNo()+"的审批记录";
        if(flag){
            if(!org.springframework.util.StringUtils.isEmpty(vo.getCcUsers())){
                List<String> ccUserList = Arrays.asList(vo.getCcUsers().split(","));
                sendUserList.addAll(ccUserList);
            }
            title = "["+vo.getCreateBy()+"]请假申请审批通过";
            content = "["+vo.getCreateBy()+"]提交学生["+vo.getStudentName()+"]的请假申请单已经审批通过，请假时间为:"+vo.getLeaveStartTime()+"~"+vo.getLeaveEndTime();

        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //发送手机通知
        jgMessageUser(vo.getCreateBy(),title);
    }


    /**
     * 处理请假审批后的业务状态
     * @param vo
     */
    private void processLeaveApply(WorkFlowVO vo){
        LambdaQueryWrapper<LeaveApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaveApplyVO::getProcessId,vo.getProcessInstanceId());
        LeaveApplyVO leaveVO = leaveApplyDao.selectOne(wrapper);
        Integer days = Integer.valueOf(leaveVO.getLeaveTotalTime());

        LeaveApplyVO applyVO = new LeaveApplyVO();
        if("leaderApprovalCountersign".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,false);
                leaveApplyDao.update(applyVO,wrapper);
            }
            if(1==vo.getFlag() && days<=1){
                applyVO.setStatus("3");//流程正常结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,true);
                leaveApplyDao.update(applyVO,wrapper);
            }

        }
        if("inChargePrincipal".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,false);
                leaveApplyDao.update(applyVO,wrapper);
            }
            if(1==vo.getFlag() && days<=2){
                applyVO.setStatus("3");//流程正常结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,true);
                leaveApplyDao.update(applyVO,wrapper);
            }
        }
        if("executePrincipal".equals(vo.getCurrNode())){
            if(0==vo.getFlag()){
                applyVO.setStatus("2");//驳回流程结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,false);
                leaveApplyDao.update(applyVO,wrapper);
            }
            if(1==vo.getFlag() && days>=3){
                applyVO.setStatus("3");//流程正常结束
                applyVO.setCurrApprovedBy("");
                addFlowNotice(leaveVO,true);
                leaveApplyDao.update(applyVO,wrapper);
            }
        }
    }

    private void addFlowNotice(LeaveApplyVO leaveVO,boolean flag) {
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(leaveVO.getCreateBy());
        String title = "["+leaveVO.getCreateBy()+"]请假申请审批不通过";
        String content = "["+leaveVO.getCreateBy()+"]提交的请假申请单审批不通过，具体内容请查看请假申请单"+leaveVO.getApplyNo()+"的审批记录";
        if(flag){
            if(!org.springframework.util.StringUtils.isEmpty(leaveVO.getCcUsers())){
                List<String> ccUserList = Arrays.asList(leaveVO.getCcUsers().split(","));
                sendUserList.addAll(ccUserList);
            }
            title = "["+leaveVO.getCreateBy()+"]请假申请审批通过";
            content = "["+leaveVO.getCreateBy()+"]提交的请假申请单已经审批通过，请假时间为:"+leaveVO.getLeaveStartTime()+"~"+leaveVO.getLeaveEndTime();

        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //发送手机通知
        jgMessageUser(leaveVO.getCreateBy(),title);
    }
    /**
     * 处理离职审批后的业务状态
     * @param vo
     */
    private void processQuitApply(WorkFlowVO vo){
        LambdaQueryWrapper<QuitApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuitApplyVO::getProcessId,vo.getProcessInstanceId());
        QuitApplyVO quitVO = quitApplyDao.selectOne(wrapper);
        QuitApplyVO applyVO = new QuitApplyVO();
        applyVO.setStatus("1");
        if(0==vo.getFlag()){
            applyVO.setStatus("2");//驳回流程结束
            applyVO.setCurrApprovedBy("");//驳回后处理人为空
            //驳回后要通知到申请人 TODO
        }
        if("executePrincipal".equals(vo.getCurrNode())){
            if(1==vo.getFlag()){
                applyVO.setStatus("3");//流程正常结束
                applyVO.setCurrApprovedBy("");//清空审批人
                //通过后要通知到申请人和抄送人
                addQuitFlowNotice(quitVO);
                //修改人员信息更新离职时间
                LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
                userWrapper.eq(SysUser::getUsername,quitVO.getCreateBy());
                SysUser sysuser = new SysUser();
//                sysuser.setStatus("0");
                sysuser.setSysuLeavedate(quitVO.getQuitDate());
                sysUserDao.update(sysuser,userWrapper);
            }
        }
        quitApplyDao.update(applyVO,wrapper);
    }

    private void addQuitFlowNotice(QuitApplyVO quitVO) {
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(quitVO.getCreateBy());
        if(!org.springframework.util.StringUtils.isEmpty(quitVO.getCcUsers())){
            List<String> ccUserList = Arrays.asList(quitVO.getCcUsers().split(","));
            sendUserList.addAll(ccUserList);
        }
        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle("["+quitVO.getCreateBy()+"]离职申请审批通过");
            workflowNotice.setContent("["+quitVO.getCreateBy()+"]提交的离职申请单已经审批通过，将于"+quitVO.getQuitDate()+"正式离职。");
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }

    }

    /**
     * 用人申请用人通知
     * @param applyVO
     * @param flag 是否通过 true 通过 ；false 不通过
     */
    private void addUsePersonFlowNotice(UsePersonApplyVO applyVO,boolean flag) {
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(applyVO.getCreateBy());
        String title = "["+applyVO.getCreateBy()+"]部门用人申请审批不通过";
        String content = "["+applyVO.getCreateBy()+"-"+applyVO.getApplyUserDeptName()+"]提交的部门用人申请单审批不通过，具体内容请查看部门用人申请单"+applyVO.getApplyNo()+"的审批记录";
        if(flag){
            if(!org.springframework.util.StringUtils.isEmpty(applyVO.getCcUsers())){
                List<String> ccUserList = Arrays.asList(applyVO.getCcUsers().split(","));
                sendUserList.addAll(ccUserList);
            }
            title = "["+applyVO.getCreateBy()+"]部门用人申请审批通过";
            content = "["+applyVO.getCreateBy()+"-"+applyVO.getApplyUserDeptName()+"]提交的部门用人申请单已经审批通过，到岗时间为:"+applyVO.getWorkDate();

        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //发送手机通知
        jgMessageUser(applyVO.getCreateBy(),title);

    }

    /**
     * 维修申请用人通知
     * @param applyVO
     * @param flag 是否通过 true 通过 ；false 不通过
     */
    private void addRepairFlowNotice(RepairApplyVO applyVO,boolean flag) {
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(applyVO.getCreateBy());
        String title = "["+applyVO.getCreateBy()+"]维修申请审批不通过";
        String content = "["+applyVO.getCreateBy()+"提交的维修申请单审批不通过，具体内容请查看部门用人申请单"+applyVO.getApplyNo()+"的审批记录";
        if(flag){
            title = "["+applyVO.getCreateBy()+"]维修申请审批通过";
            content = "["+applyVO.getCreateBy()+"]提交的维修申请单已经审批通过，物品:["+applyVO.getArticleName()+"]已维修完毕！";
        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //发送手机通知
        jgMessageUser(applyVO.getCreateBy(),title);

    }

    private void addGoodsBuyNotice(GoodsBuyApplyVO applyVO,boolean flag){
        List<String> sendUserList = new ArrayList<>();
        sendUserList.add(applyVO.getCreateBy());
        String title = "["+applyVO.getCreateBy()+"]物品申购申请审批不通过";
        String content = "["+applyVO.getCreateBy()+"提交的物品申购申请单审批不通过，具体内容请查看物品申购申请单"+applyVO.getApplyNo()+"的审批记录";
        if(flag){
            title = "["+applyVO.getCreateBy()+"]物品申购申请审批通过";
            content = "["+applyVO.getCreateBy()+"]提交的物品申购申请单已经审批通过完毕！";
            if(!org.springframework.util.StringUtils.isEmpty(applyVO.getCcUsers())){
                List<String> ccUserList = Arrays.asList(applyVO.getCcUsers().split(","));
                sendUserList.addAll(ccUserList);
            }
        }

        WorkflowNotice workflowNotice = null;
        for(int i=0;i<sendUserList.size();i++){
            workflowNotice = new WorkflowNotice();
            workflowNotice.setTitle(title);
            workflowNotice.setContent(content);
            workflowNotice.setStatus("0");
            workflowNotice.setNoticeUser(sendUserList.get(i));
            workflowNoticeService.insert(workflowNotice);
        }
        //发送手机通知
        jgMessageUser(applyVO.getCreateBy(),title);
    }

    private void jgMessageUser(String username,String title){
        LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(SysUser::getUsername,username);
        SysUser user = sysUserDao.selectOne(userWrapper);
        List<String> regIdList= new ArrayList<>();
        regIdList.add(user.getRegistrationId());
        asyncConfig.sendJGMessage("审批结果",title,regIdList);
    }

    private void jgMessageStudent(String title,String content,StudentInfo info){
        LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();

        SchoolClassVO classVO = schoolClassDao.selectById(info.getClassId());
        userWrapper.eq(SysUser::getUsername,classVO.getClassTeacherId());
        SysUser user = sysUserDao.selectOne(userWrapper);

        List<String> regIdList= new ArrayList<>();
        regIdList.add(info.getStRegistrationId());//学生
        regIdList.add(info.getGuRegistrationId());//家长
        regIdList.add(user.getRegistrationId());//老师
        //添加家长通知内容
        FamilyNotice familyNotice = new FamilyNotice();
        familyNotice.setNoticeUser(info.getGuardianPhoneNumber());
        familyNotice.setTitle("请假审批通知");
        familyNotice.setType("1");
        familyNotice.setNoticeTime(new Date());
        familyNotice.setContent(content);
        familyNotice.setStudentName(info.getName());
        familyNotice.setStudentPhone(info.getPhone());
        familyNotice.setStudentClass(info.getClassId()+"");
        familyNoticeDao.insert(familyNotice);
        //通知班主任
        WorkflowNotice workflowNotice = new WorkflowNotice();
        workflowNotice.setTitle(title);
        workflowNotice.setContent(content);
        workflowNotice.setStatus("0");
        workflowNotice.setNoticeUser(user.getUsername());
        workflowNoticeService.insert(workflowNotice);
        asyncConfig.sendJGMessage("审批结果",title,regIdList);
    }

}
